﻿
Namespace Statusmeldungen

    Public Class Statusmeldungscontroller

#Region "Singleton implementation"





        Private Shared _instance As Statusmeldungscontroller


        Public Shared Sub SetToolStripStatusLabel(ByVal label As System.Windows.Forms.ToolStripStatusLabel)

            If _instance Is Nothing Then
                _instance = New Statusmeldungscontroller(label)
            Else
                _instance.Label = label
            End If

        End Sub


        Public Overloads Shared Sub SetLabelText(ByVal message As String)

            If _instance IsNot Nothing Then _
                _instance.InternalSetLabelText(message, Drawing.Color.Black, DEFAULT_DURATION)

        End Sub


        Public Overloads Shared Sub SetLabelText(ByVal message As String, ByVal color As System.Drawing.Color)

            If _instance IsNot Nothing Then _
                _instance.InternalSetLabelText(message, color, DEFAULT_DURATION)

        End Sub


        Public Overloads Shared Sub SetLabelText(ByVal message As String, ByVal color As System.Drawing.Color, ByVal duration As Integer)

            If _instance IsNot Nothing Then _
                _instance.InternalSetLabelText(message, color, duration)

        End Sub


#End Region

        Private Const DEFAULT_DURATION As Integer = 1000


        Private _label As System.Windows.Forms.ToolStripStatusLabel


        Private WithEvents _timer As System.Windows.Forms.Timer


        Private _logger As Global.log4net.ILog


        Private Sub New(ByVal label As System.Windows.Forms.ToolStripStatusLabel)

            _logger = Global.log4net.LogManager.GetLogger(GetType(Statusmeldungscontroller))


            _logger.DebugFormat("{0} wurde initialisiert...", GetType(Statusmeldungscontroller).ToString())


            _label = label
            _timer = New System.Windows.Forms.Timer

            _timer.Interval = DEFAULT_DURATION
        End Sub


        Public Property Label As System.Windows.Forms.ToolStripStatusLabel
            Get
                Return _label
            End Get
            Set(ByVal value As System.Windows.Forms.ToolStripStatusLabel)
                _label = value
            End Set
        End Property


        Private Sub InternalSetLabelText(ByVal message As String, _
                                         ByVal color As System.Drawing.Color,
                                         ByVal duration As Integer)


            _label.Text = message
            _label.ForeColor = color

            _timer.Interval = duration

            _logger.DebugFormat("Labeltext gesetzt. Message: '{0}'; Color: '{1}'; Duration: '{2}'.", _
                                New Object() {message, color.ToString(), duration.ToString()})

            _timer.Start()

        End Sub


        Private Sub _timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles _timer.Tick

            _label.Text = ""
            _label.ForeColor = Drawing.Color.Black
            _timer.Stop()

        End Sub


    End Class

End Namespace
